<?php

namespace app\supplier\model\employ;

use app\common\model\employ\Employ as EmployModel;
use app\common\model\region\Region;

class Employ extends EmployModel
{
    public function getList($params, $shop_supplier_id)
    {
        $model = $this;

        $model = $this->setWhere($model, $params);
        // 查询列表数据
        return $model->where('is_delete', '=', '0')
            ->where("shop_supplier_id", $shop_supplier_id)->withAttr('city_name', function ($value, $data) {
                return Region::getMergerName($data["city_id"]);
            })
            ->append(['city_name'])
            ->order(['create_time' => 'desc'])
            ->paginate($params);
    }

    public function setWhere($model, $data)
    {
        if (isset($params['search']) && $params['search']) {
            $model = $model->where('name|identity_num|phone', 'like', '%' . $params['search'] . '%');
        }
        if (isset($data['create_time']) && $data['create_time'] != '') {
            $sta_time = array_shift($data['create_time']);
            $end_time = array_pop($data['create_time']);
            $model = $model->whereBetweenTime('create_time', $sta_time, date('Y-m-d 23:59:59', strtotime($end_time)));
        }
        if (isset($data['status']) && $data['status'] !== '') {
            $model = $model->where('status', $data['status']);
        }
        return $model;
    }
}